﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32;
using System.Drawing;

namespace PhotoOfTheDay.Types
{
    public static class Wallpaper
    {
        const int SPI_SETDESKWALLPAPER = 20;
        const int SPIF_UPDATEINIFILE = 0x01;
        const int SPIF_SENDWININICHANGE = 0x02;

        //const int AD_APPLY_SAVE = 0x01;
        //const int AD_APPLY_HTMLGEN = 0x02;
        //const int AD_APPLY_REFRESH = 0x04;
        //const int AD_APPLY_ALL = (AD_APPLY_SAVE | AD_APPLY_HTMLGEN | AD_APPLY_REFRESH);
        //const int AD_APPLY_FORCE = 0x08;
        //const int AD_APPLY_BUFFERED_REFRESH = 0x010;
        //const int AD_APPLY_DYNAMICREFRESH = 0x020;

        //[DllImport("Shell32.dll", CharSet = CharSet.Auto)]
        //static extern int SetWallpaper(string pwszWallpaper, int dwReserved);

        //[DllImport("Shell32.dll", CharSet = CharSet.Auto)]
        //static extern int ApplyChanges(int dwFlags);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

        public enum Style : int
        {
            Tiled,
            Centered,
            Stretched
        }

        public static void Set(Bitmap bmp, Style style)
        {
            //Don't set wallpaper if wallpaper is not own by PoD
            if (GetWallpaper() != Global.WallpaperPath) return;

            //check if copyrighted
            if (Global.IsCopyrighted) return;

            bmp.Save(Global.WallpaperPath, System.Drawing.Imaging.ImageFormat.Bmp);

            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
            if (style == Style.Stretched)
            {
                key.SetValue(@"WallpaperStyle", 2.ToString());
                key.SetValue(@"TileWallpaper", 0.ToString());
            }

            if (style == Style.Centered)
            {
                key.SetValue(@"WallpaperStyle", 1.ToString());
                key.SetValue(@"TileWallpaper", 0.ToString());
            }

            if (style == Style.Tiled)
            {
                key.SetValue(@"WallpaperStyle", 1.ToString());
                key.SetValue(@"TileWallpaper", 1.ToString());
            }

            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, Global.WallpaperPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
            
        }

        public static string GetWallpaper()
        {
            //Get the key that holds the wallpaper
            RegistryKey wallPaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", false);
            //Set the wallpaper path to the one in the key
            string WallpaperPath = wallPaper.GetValue("WallPaper").ToString();
            //Close the reg key
            wallPaper.Close();
            //Return the wallpaper
            return WallpaperPath;
        }

        public static void SetOwner()
        {
            Bitmap newBMP = (Bitmap)Bitmap.FromFile(GetWallpaper());
            newBMP.Save(Global.WallpaperPath, System.Drawing.Imaging.ImageFormat.Bmp);
            newBMP.Dispose();
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, Global.WallpaperPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        }

    }
}
